package managers.socialNetworkManagers;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Timer;

import javax.swing.ImageIcon;

import managers.DBManager;
import managers.socialNetworkManagers.PeriodicService;
import utils.SocialUserData;

public class SocialNetworkManager {

	private static SocialNetworkManager sharedInstance = new SocialNetworkManager();

	private HashMap<Integer, SocialUserData> socialUsersData = new HashMap<Integer, SocialUserData>();

	private int userId;
	private SocialNetworkFeedbackManager feedbackManager;

	private HashMap<String, PeriodicService> periodicServices = new HashMap<String, PeriodicService>();

	private boolean parametersSet = false;



	private SocialNetworkManager () {
	}

	public void setParameters (int userId, SocialNetworkFeedbackManager feedbackManager) {
		this.userId = userId;
		this.feedbackManager = feedbackManager;

		parametersSet = true;
	}

	public void initialize () throws SQLException, IOException, SocialNetworkInitializingException {

		if (!parametersSet )
			throw new SocialNetworkInitializingException ("Parameters of social network are not set. Set before initializing.");

		updateUsers(DBManager.getConnection());
		SocialDataUpdateMethods.updateMiniatures(DBManager.getConnection(), this);

		feedbackManager.fillUsersList(socialUsersData);

		Collection<PeriodicService> ps = periodicServices.values();
		Iterator<PeriodicService> it2 = ps.iterator();
		while (it2.hasNext()) {
			PeriodicService temp = it2.next();
			new Timer().schedule(temp, temp.getPeriod(), temp.getPeriod());
		}
	}

	
	private void updateUsers(Connection conn) throws SQLException {

		Statement stmt = conn.createStatement();
		String query = "select id, firstname, secondname, surname, nickname, usertype, mood, personalmessage from spotmessenger.USERS";
		ResultSet rs = stmt.executeQuery(query);

		while (rs.next()) {
			SocialUserData user = new SocialUserData(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),
					rs.getString(5), rs.getInt(6), rs.getString(7), rs.getString(8));

			socialUsersData.put(user.getUserId(), user);
		}

		DBManager.closeResources(stmt, rs);
		SocialNetworkManager.getSharedInstance().getFeedbackManager().update();
	}


	public void addPeriodicService (PeriodicService periodicService) {
		periodicServices.put(periodicService.getName(), periodicService);
	}


	public SocialNetworkFeedbackManager getFeedbackManager() {
		return feedbackManager;
	}

	public SocialUserData getUser (int userId) {
		return socialUsersData.get(userId);
	}

	public HashMap<Integer, SocialUserData> getUsers () {
		return socialUsersData;
	}

	public static SocialNetworkManager getSharedInstance() {
		return sharedInstance;
	}

	public int getUserId() {
		return userId;
	}


}
